//: C05:Urand.h {-bor}
// Unique randomizer.
#ifndef URAND_H
#define URAND_H
#include <bitset>
#include <cstddef>
#include <cstdlib>
#include <ctime>
using std::bitset;
using std::size_t;

template <size_t UpperBound> class Urand {
  bitset<UpperBound> used;

public:
  Urand() { srand(time(0)); } // Randomize
  size_t operator()();        // The "generator" function
};

template <size_t UpperBound> inline size_t Urand<UpperBound>::operator()() {
  if (used.count() == UpperBound) {
    used.reset(); // Start over (clear bitset)
  }
  size_t newval;
  while (used[newval = rand() % UpperBound]) {
    ;
  }
  used[newval] = true;
  return newval;
}
#endif